/** 请求工具 */
export class ApiUtils {
    /** 
     * 提交
     * @param {Object} options 参数配置
     * @param {String} options.loadingName  loading名称
     * @param {Function} options.handler    执行函数
     * @param {Function} options.authen     认证函数
     * @example 
     *      const _resetFn = () => { };
     *      this.$ApiUtils.submit.call(this, {
                loadingName: "submitOrderLoading",
                authen: () => {
                    let msg;
                    if (false) msg = "";
                    msg && this.$msg(msg, "e");
                    return !msg;
                },
                handler: async () => {
                    try {
                        const query = {
                            xxx: undefined,
                        };
                        await this.$post("", query);
                        _resetFn();
                        this.$msg("提交成功");
                    } catch (error) { this.$msg((error || {}).msg || '失败', 'e'); }
                }
            });
     */
    static async submit(options = {}) {
        const { loadingName, handler, authen } = options;
        if (!loadingName) throw "无loading 名"
        if (!handler) throw "无执行函数"
        if (!authen) throw "无认证函数"
        if (this[loadingName] || !authen()) return;
        this[loadingName] = true;
        const startTime = Date.now();
        try {
            await handler();
        } catch (e) { console.debug('submitUtils error=', e) }
        const endTime = 800 - Date.now() - startTime;
        setTimeout(() => { this[loadingName] = false; }, endTime < 0 ? 0 : endTime)
    }
}